package cm.mefobe.android.telecomcost.view;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

public class LabeledEditText extends LinearLayout {

	private Context _context;
	private TextView _label;
	private EditText _txt;
	
	public LabeledEditText(Context context) {
		super(context);
		Log.i("LabeledEditText", "creating the picker view");
		_context = context;
	}

	public EditText getEditText() {
		if (_txt == null) {
			throw new UnsupportedOperationException(
					"LabeledEditText edit text is not set");
		}

		return _txt;
	}

	public void setLabel(String label) {
		if (_label == null) {
			setLabeledEditText();
		}

		_label.setText(label);
	}

	private void setLabeledEditText() {
		if (isInEditMode()) {
			Log.i("LabeledEditText", "isInEditMode");
		} else {
			if (_label == null) {
				Log.i("LabeledEditText", "adding views...");
				setOrientation(LinearLayout.HORIZONTAL);

				_label = new TextView(_context);
				_txt = new EditText(_context);				
								
				addView(_label);
				addView(_txt);	
			} else {
				Log.i("LabeledEditText", "LabeledEditText already set...we set the layout of the textbox");
				_txt.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
			}
		}
	}

	public LabeledEditText(Context context, AttributeSet attrs) {
		super(context, attrs);
		Log.i("LabeledEditText", "creating the picker view with AttributeSet");

		_context = context;
	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		Log.i("LabeledEditText", "onLayout");
		super.onLayout(changed, l, t, r, b);
		setLabeledEditText();
	}

	@Override
	protected void onAttachedToWindow() {
		Log.i("LabeledEditText", "onAttachedToWindow");
		super.onAttachedToWindow();
	}

	@Override
	protected void onDraw(Canvas canvas) {
		Log.i("LabeledEditText", "onDraw");
		super.onDraw(canvas);
	}

	@Override
	protected void onFinishInflate() {
		Log.i("LabeledEditText", "onFinishInflate");
		super.onFinishInflate();
		setLabeledEditText();
	}

	@Override
	public void updateViewLayout(View view,
			android.view.ViewGroup.LayoutParams params) {
		Log.i("LabeledEditText", "updateViewLayout");
		super.updateViewLayout(view, params);
	}

	public String getText() {
		return _txt.getText().toString();
	}

	public void setText(String text) {
		_txt.setText(text);
	}
}